package com.basis.message;

import java.util.ArrayList;
import java.util.List;

public class FilterClauseParseUtil {
	private List<String> paramNames = null;
	private String firstWhere = null;
	public FilterClauseParseUtil() {
		paramNames = new ArrayList<String>();
	}
	
	public void parseWhere(String where) {
		firstWhere = where;
		String[] wheres = where.toUpperCase().split(" AND ");
		for (String w : wheres) {
			parseOr(w);
		}
	}
	
	private void parseOr(String where) {
		String[] wheres = where.split(" OR ");
		for (String w : wheres) {
			parseParamName(w);
		}
	}
	
	private void parseParamName(String where) {
		String name = null;
		if (where.indexOf("<") > 0) {
			name = where.split("<")[0].trim();
		} else if (where.indexOf(">") > 0) {
			name = where.split(">")[0].trim();
		} else if (where.indexOf("=") > 0) {
			name = where.split("=")[0].trim();
		} else if (where.indexOf(" IN ") > 0) {
			name = where.split(" IN ")[0].trim();
		} else if (where.indexOf(" LIKE ") > 0) {
			name = where.split(" LIKE ")[0].trim();
		}
		String fw = firstWhere.toUpperCase();
		int index = fw.indexOf(name);
		name = firstWhere.substring(index, index + name.length());
		paramNames.add(name);
	}
	
	public List<String> getParamNames() {
		return paramNames;
	}
}
